<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      inline
      v-show="showSearch"
      label-width="68px"
      @submit.native.prevent
    >
      <el-form-item label="点位名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入停车点位或小区名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['weiriya:area:add']"
        >
          新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['weiriya:area:remove']"
        >
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['weiriya:area:export']"
        >
          导出
        </el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @query-table="getList" />
    </el-row>

    <el-table
      v-loading="loading"
      :data="areaList"
      border
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="停车点位名称" align="center" prop="name" />
      <el-table-column label="停车点位地址" align="center" prop="address" />
      <el-table-column label="管理人员" align="center" prop="nickName" />
      <el-table-column label="联系电话" align="center" prop="phonenumber" />
      <el-table-column label="所属公司" align="center" prop="companyName" />
      <el-table-column label="车辆信息" align="center" width="100">
        <template slot-scope="scope">
          <span class="link" @click="handleCar(scope.row)"> 查看 </span>
        </template>
      </el-table-column>
      <el-table-column label="订单信息" align="center" width="100">
        <template slot-scope="scope">
          <span class="link" @click="handleOrder(scope.row)"> 查看 </span>
        </template>
      </el-table-column>
      <el-table-column label="用户信息" align="center" width="100">
        <template slot-scope="scope">
          <span class="link" @click="handleUser(scope.row)"> 查看 </span>
        </template>
      </el-table-column>
      <el-table-column label="收费设置" align="center" width="220">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-coin"
            @click="handleVipPrice(scope.row, 1)"
            v-hasPermi="['weiriya:area:edit']"
          >
            充电/停车
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-coin"
            @click="handleVipPrice(scope.row, 2)"
            v-hasPermi="['weiriya:area:edit']"
          >
            停车
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-coin"
            @click="handleTemp(scope.row)"
            v-hasPermi="['weiriya:area:edit']"
          >
            临时收费
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['weiriya:area:edit']"
          >
            修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['weiriya:area:remove']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改停车点位 -->
    <el-dialog
      :title="form.id ? '修改停车点位' : '新增停车点位'"
      :visible.sync="open"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="停车点位名称" prop="name">
          <el-input
            v-model="form.name"
            maxlength="20"
            placeholder="请输入停车点位或小区名称"
            clearable
          />
        </el-form-item>
        <el-form-item label="停车点位地址" prop="address">
          <el-input
            v-model="form.address"
            max="50"
            placeholder="请输入停车点位地址"
            clearable
          />
        </el-form-item>
        <el-form-item label="所属公司" prop="companyId">
          <el-input
            v-model="form.companyName"
            max="50"
            :disabled="form.id"
            placeholder="请选择所属公司"
            readonly
            @focus="handleCompany"
          />
        </el-form-item>
        <el-form-item label="管理人员" prop="userId">
          <el-select
            v-model="form.userId"
            placeholder="请选择管理人员"
            style="width: 100%"
            clearable
            filterable
            @change="handleNumber"
          >
            <el-option
              v-for="item in userList"
              :key="item.userId"
              :label="item.nickName"
              :value="item.userId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input
            v-model="form.phone"
            maxlength="11"
            disabled
            placeholder="请选择管理人员"
            clearable
          />
        </el-form-item>
        <el-form-item label="最低预存金额" prop="lowestAmount">
          <el-input
            v-model="form.lowestAmount"
            placeholder="请输入最低预存金额"
            type="number"
            clearable
          >
            <span slot="suffix">元</span>
          </el-input>
        </el-form-item>
        <el-form-item label="首次押金" prop="firstDeposit">
          <el-input
            v-model="form.firstDeposit"
            type="number"
            placeholder="请输入首次押金"
            clearable
          >
            <span slot="suffix">元</span>
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
    <Company-dialog v-model="companyShow" single @comfirm="comfirmCompany" />
    <Car-dialog v-model="carShow" :id="chooseId" />
    <Order-dialog v-model="orderShow" :id="chooseId" />
    <User-dialog v-model="userShow" :id="chooseId" />
    <Vip-dialog v-model="vipShow" :id="chooseId" :type="chooseType" />
    <Temporary-dialog v-model="temporaryShow" :id="chooseId" />
  </div>
</template>

<script>
import { listArea, getArea, delArea, addArea, updateArea } from "@/api/weiriya/area";
import { listUser } from "@/api/system/user";
import CompanyDialog from "@/components/CompanyDialog/index";
import CarDialog from "@/views/weiriya/area/cp/CarDialog";
import OrderDialog from "@/views/weiriya/area/cp/OrderDialog";
import UserDialog from "@/views/weiriya/area/cp/UserDialog";
import VipDialog from "@/views/weiriya/area/cp/VipDialog";
import TemporaryDialog from "@/views/weiriya/area/cp/TemporaryDialog";

export default {
  name: "Area",
  components: {
    CompanyDialog,
    CarDialog,
    OrderDialog,
    UserDialog,
    VipDialog,
    TemporaryDialog,
  },
  data() {
    return {
      loading: false, // 遮罩层
      ids: [], // 选中数组
      single: true, // 非单个禁用
      multiple: true, // 非多个禁用
      showSearch: true, // 显示搜索条件
      total: 0, // 总条数
      areaList: [], // 数据列表
      open: false, // 是否显示弹出层
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null, // 停车点位名称
      },
      form: {}, // 表单参数
      rules: {
        name: [{ required: true, message: "请输入停车点位或小区名称", trigger: "blur" }],
        address: [{ required: true, message: "请输入停车点位地址", trigger: "blur" }],
        companyId: [{ required: true, message: "请选择所属公司", trigger: "blur" }],
        userId: [{ required: true, message: "请选择管理人员", trigger: "blur" }],
        // phone: [
        //   { required: true, message: "请输入联系电话", trigger: "blur" },
        //   {
        //     pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
        //     message: "请输入正确的手机号",
        //     trigger: "blur",
        //   },
        // ],
        lowestAmount: [
          { required: true, message: "请设置最低金额", trigger: "blur" },
          { pattern: /^[0-9]*$/, message: "请设置正确的金额", trigger: "blur" },
        ],
        firstDeposit: [
          { required: true, message: "请设置押金金额", trigger: "blur" },
          { pattern: /^[0-9]*$/, message: "请设置正确的金额", trigger: "blur" },
        ],
      }, // 表单校验
      userList: [], // 用户信息列表
      chooseId: null, // 选中的停车点位id
      companyShow: false, // 公司信息弹窗
      carShow: false, // 车辆信息弹窗
      orderShow: false, // 订单信息弹窗
      userShow: false, // 用户信息弹窗
      chooseType: 1, // 类型 1 充电/停车 2 停车
      vipShow: false, // 充电/停车弹窗
      nonVipShow: false, // 停车弹窗
      temporaryShow: false, // 临时收费弹窗
    };
  },
  created() {
    this.getList();
  },
  watch: {
    "form.companyId": {
      handler(val) {
        if (!val) {
          this.userList = [];
        }
      },
      immediate: true,
      deep: true,
    },
  },
  methods: {
    // 查询停车点位列表
    getList() {
      this.loading = true;
      listArea(this.queryParams)
        .then((res) => {
          this.areaList = res.rows;
          this.total = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 取消
    handleCancel() {
      this.open = false;
      this.reset();
    },
    // 打开公司弹窗
    handleCompany() {
      this.companyShow = true;
    },
    // 打开车辆弹窗
    handleCar(row) {
      this.chooseId = row.id;
      this.carShow = true;
    },
    // 打开订单弹窗
    handleOrder(row) {
      this.chooseId = row.id;
      this.orderShow = true;
    },
    // 打开用户弹窗
    handleUser(row) {
      this.chooseId = row.id;
      this.userShow = true;
    },
    // 打开价格配置弹窗
    handleVipPrice(row, type = 1) {
      this.chooseType = type;
      this.chooseId = row.id;
      this.vipShow = true;
    },
    // 打开临时收费弹窗
    handleTemp(row) {
      this.chooseId = row.id;
      this.temporaryShow = true;
    },
    // 确认公司信息
    comfirmCompany(val) {
      this.form.companyId = val.id;
      this.form.companyName = val.name;
      this.getUser();
      this.userList = [];
      this.form.phone = null;
      this.form.userId = null;
      this.form.userName = null;
    },
    // 选择用户信息
    handleNumber(val) {
      if (val) {
        this.form.phone = this.userList.find((item) => item.userId == val).phonenumber;
      } else {
        this.form.phone = null;
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null, // 停车点位名称
        address: null, // 停车点位地址
        companyId: null, // 所属公司id
        companyName: null, // 所属公司
        userId: null, // 管理人员id
        userName: null, // 管理人员姓名
        phone: null, // 管理人员电话
        carCount: null, // 车辆数量
        orderCount: null, // 订单数量
        userCount: null, // 用户数量
        lowestAmount: 0, // 最低金额
        firstDeposit: 0, // 首次押金
      };
      this.resetForm("form");
    },
    // 查询用户信息列表
    getUser() {
      let data = {
        pageNum: 1,
        pageSize: 999,
        companyId: this.form.companyId,
      };
      listUser(data).then((res) => {
        this.userList = res.rows;
      });
    },
    // 搜索按钮
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 重置
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 新增
    handleAdd() {
      this.reset();
      this.open = true;
    },
    // 修改
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getArea(id).then((res) => {
        res.data.userId = parseInt(res.data.userId);
        res.data.phone = res.data.phonenumber;
        delete res.data.phonenumber;
        this.form = res.data;
        this.getUser();
        this.open = true;
      });
    },
    // 提交表单
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id) {
            updateArea(this.form).then((res) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addArea(this.form).then((res) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 删除按钮操作
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm(`是否确认删除停车点位编号为${ids}的数据项？`).then(() => {
        delArea(ids).then((res) => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        });
      });
    },
    // 导出
    handleExport() {
      this.download(
        "weiriya/area/export",
        {
          ...this.queryParams,
        },
        `area_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
.link {
  color: #409eff;
  cursor: pointer;
}
</style>
